var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var http = require('http');

if(cluster.isMaster){
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    console.log(cluster.workers);

}else if(cluster.isWorker){
  console.log('[worker] ' + "worker" + cluster.worker.id + " started, pid:" + process.pid);

  cluster.worker.on('exit',function(msg){
        console.log('[worker] worker' + cluster.worker.id + ' received KILL:' + msg);
  });

  //防止退出
    http.createServer(function (req, res) {
        var response = 'worker received request, id:' + cluster.worker.id + ',pid:' + process.pid;
        console.log(response);
        res.writeHead(200, { "content-type": "text/html" });
        res.end(response);
    }).listen(5000);
}